﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// quanxian 的摘要说明
    /// </summary>
    public class quanxian : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            //取得处事类型
            string action = DTRequest.GetQueryString("action");

            switch (action)
            {
                case "gongqiu": //提交求购
                    gongqiu(context);
                    break;
            }
        }

        #region 提交供应信息===========================
        private void gongqiu(HttpContext context)
        {

            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd))
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }
            Model.users userModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            if (userModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"您的登录信息有误，请退出系统后重新登录！\"}");
                return;
            }

            //判断当前用户是否有权限
            Model.user_groups groupModel = new BLL.user_groups().GetModel(userModel.group_id);
            if (groupModel == null)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"出错，无法获取用户组信息！\"}");
                return;
            }

            if (groupModel.gongqiu_add == 0)
            {
                context.Response.Write("{\"status\":1}");
                return;
            }
            else if (groupModel.gongqiu_add == -1)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"<p>抱歉，您没有发布信息的权限！</p>\"}");
                return;
            }

            int gongying_count = new BLL.gongying().GetCount("user_id=" + userModel.id + " and datediff(day,add_time,getdate())=0");
            int qiugou_count = new BLL.qiugou().GetCount("user_id=" + userModel.id + " and datediff(day,add_time,getdate())=0");
            int total_count = gongying_count + qiugou_count;
            if (total_count >= groupModel.gongqiu_add)
            {
                context.Response.Write("{\"status\":0, \"msg\":\"<p>抱歉，您的权限不足以继续发布信息！</p><p>1、您当前的身份是" + groupModel.title + "；</p><p>2、您每天最多可以发布" + groupModel.gongqiu_add.ToString() + "条供求信息；</p><p>3、您今天已发布了" + total_count.ToString() + "条信息。</p>\"}");
                return;
            }
            context.Response.Write("{\"status\":1}");
            return;
        }
        #endregion

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}